package com.zzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzx.common.domain.CommonResult;
import com.zzx.mapper.UserMapper;
import com.zzx.pojo.User;
import com.zzx.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/10/3 10:36
 */
@Service
public class UserServiceImpl implements IUserService
{
    @Resource
    UserMapper userMapper;

    @Override
    public CommonResult getUserById(int userId)
    {
        User user = userMapper.selectById(userId);
        if (user != null)
        {
            return CommonResult.ok().data("user", user);
        }
        return CommonResult.error();
    }

    @Override
    public CommonResult userLogin(User user)
    {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUserName, user.getUserName());
        wrapper.eq(User::getPassword, user.getPassword());
        User queryUser = userMapper.selectOne(wrapper);
        if (queryUser != null)
        {
            return CommonResult.ok().data("user", queryUser);
        }
        return CommonResult.error();
    }

    @Override
    public CommonResult editUserBalance(User user)
    {
        int i = userMapper.updateById(user);
        if (i == 1)
        {
            return CommonResult.ok();
        }
        return CommonResult.error();
    }
}